package structure;

import java.util.ArrayList;

import algorithm.miscellaneous;

public class bigProduction {
	
	private ArrayList<item> items;
	private String leftside;
	private String rightside;
	private ArrayList<String> lookahead;
	private int index;
	private boolean duplicated;
	
	public bigProduction(){
		
		this.items= new ArrayList<item>();
		this.leftside= "";
		this.rightside= "";
		this.lookahead= new ArrayList<String>();
		this.index= -1;
		this.duplicated=false;
	}
	
	public bigProduction(production production){
		
		this.leftside= production.getLeftSide();
		this.rightside= production.getRightSide().get(0);
		this.items= new ArrayList<item>();
		this.lookahead= new ArrayList<String>();
		this.createItem();
		this.index= -1;
		this.duplicated=false;
		
	}
	
	//ALERT: attenzione ci potrebbero essere errori sulla copia dell'ArrayList
	public bigProduction(bigProduction big){
		
		this.leftside= big.getLeftside();
		this.rightside= big.getRightside();
		this.items= miscellaneous.cloneArrayItem(big.getItems());
		this.lookahead= new ArrayList<String>();
		this.index= -1;
		this.duplicated=false;
	}
	
	//
	public void createItem(){
		
		if(this.rightside.equals("e")){
			item tmp_item= new item(this.leftside, ".", null);
			this.items.add(tmp_item);
		}
		else{
			int size= this.rightside.length()+1;
			char[] right= this.rightside.toCharArray();
			char[] tmp_right= new char[size];
			for(int i=0;i<size;i++){
				
				for(int j=0;j<i;j++){
					
					tmp_right[j]= right[j];
				}
				tmp_right[i]= '.';
				for(int j=i;j<size-1;j++){
					tmp_right[j+1]= right[j];
				}
				
				String tmp_str_right= String.valueOf(tmp_right);
				item tmp_item= new item(this.leftside, tmp_str_right, null);
				this.items.add(tmp_item);
			}
			
		}
	}
	
	public void setLookahead(ArrayList<String> lookahead){
		
		this.lookahead.clear();
		this.lookahead.addAll(lookahead);
		
		for(item i : this.items){
			
			i.setLookahead(lookahead);
		}
	}
	
	public void setIndex(int i){
		
		this.index= i;
	}
	
	public void setDuplicate(){
		
		this.duplicated= true;
	}
	
	public int getIndex(){
		
		return this.index;
	}
	
	public ArrayList<String> getLookahead(){
		
		return this.lookahead;
	}
	
	public ArrayList<item> getItems(){
		
		return this.items;
	}
	
	public item getItem(int i){
		
		return this.items.get(i);
	}
	
	public String getLeftside(){
		
		return this.leftside;
	}
	
	public String getRightside(){
		
		return this.rightside;
	}
	
	public boolean hasLookahead(){
		
		if(this.lookahead.isEmpty()){
			
			return false;
		}
		
		return true;
	}
	
	public boolean isDuplicate(){
		
		return this.duplicated;
	}
	
	public boolean sameAs(bigProduction big){
		
		boolean toReturn= false;
		//stesso leftside
		if(this.leftside.equals(big.leftside)){
			
			if(this.items.size() == big.getItems().size()){
				
				boolean same= true;
				for(int i=0; i<this.items.size(); i++){
					
					if(!this.items.get(i).sameAs(big.getItems().get(i))){
						
						same= false;
						break;
					}
				}
				
				toReturn= same;
			}
		}
		
		return toReturn;
	}
	
	public void printItAll(){
		//System.out.println("BIG PRODUCTION");
		for(item i : this.items){
			
			if(!i.isComplete()){
				i.printItAll();
				System.out.print("-->" + i.getAfterDot() + " ");
			}
			else{

				i.printItAll();
			}
		}
		System.out.println("\n");
		//System.out.println("\nBIG PRODUCTION END");
	}
}
